package com.hxzy.service.impl;

import com.hxzy.util.MybatisUtil;
import com.hxzy.dto.storehouseDTO;
import com.hxzy.entity.storehouse;
import com.hxzy.mapper.storehouseMapper;
import com.hxzy.service.storehouseService;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: YangYong
 * @Date: 2022/3/8 17:13
 */
public class storehouseServiceIpml implements storehouseService {
    @Override
    public int insert(storehouse storehouse) {
        int count=0;
        try (SqlSession sqlSession = MybatisUtil.getSqlSessionFactory().openSession()) {
            storehouseMapper mapper = sqlSession.getMapper(storehouseMapper.class);
            count = mapper.insert(storehouse);
            sqlSession.commit();
        }
        return count;
    }

    @Override
    public int update(storehouse storehouse) {
        int count=0;
        try (SqlSession sqlSession = MybatisUtil.getSqlSessionFactory().openSession()) {
            storehouseMapper mapper = sqlSession.getMapper(storehouseMapper.class);
            count = mapper.update(storehouse);
            sqlSession.commit();
        }
        return count;
    }

    @Override
    public int delete(String sh_id) {
        int count=0;
        try (SqlSession sqlSession = MybatisUtil.getSqlSessionFactory().openSession()) {
            storehouseMapper mapper = sqlSession.getMapper(storehouseMapper.class);
            count = mapper.delete(sh_id);
            sqlSession.commit();
        }
        return count;
    }

    @Override
    public List<storehouse> findAll(storehouseDTO storehouseDTO) {
        List<storehouse> list=new ArrayList<>();
        try (SqlSession sqlSession = MybatisUtil.getSqlSessionFactory().openSession()) {
            storehouseMapper mapper = sqlSession.getMapper(storehouseMapper.class);
            list = mapper.findAll(storehouseDTO);
            sqlSession.commit();
        }
        return list;
    }

    @Override
    public storehouse searchByName(String sh_name) {
        storehouse storehouse=null;
        try (SqlSession sqlSession = MybatisUtil.getSqlSessionFactory().openSession()) {
            storehouseMapper mapper = sqlSession.getMapper(storehouseMapper.class);
            storehouse = mapper.searchByName(sh_name);
            sqlSession.commit();
        }
        return storehouse;
    }
}
